﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;

namespace Runda.EShop.Common.Utility
{
    public class EnumHelper
    {
        #region 字典
        public enum DictionaryType
        {  
            
        }
        #endregion 

        #region User Type
        /// <summary>
        /// 用户类型
        /// </summary>
        public enum UserType
        { 
            [EnumDesc("公司")]
            Administrator = 0,
            [EnumDesc("客户")]
            Customer = 1, 
        }
        /// <summary>
        /// 企业类型
        /// </summary>
        public enum EnterpriseType
        {
            /// <summary>
            /// 终端用户(医院)
            /// </summary>
            [EnumDesc("终端用户")]
            Terminal = 0,
            /// <summary>
            /// 经销商
            /// </summary>
            [EnumDesc("经销商")]
            Agent = 1,
        }

        #endregion    

        #region  日志类型
        /// <summary>
        /// 操作日志类型
        /// </summary>
        public enum OperationType
        { 
            [EnumDesc("无")]
            None,
            [EnumDesc("增加")]
            Add,
            [EnumDesc("删除")]
            Delete ,
            [EnumDesc("查询")]
            Select,
            [EnumDesc("编辑")]
            Edit,
        }
        /// <summary>
        /// 同步类型
        /// </summary>
        public enum SyncLogType
        {
            [EnumDesc("产品同步")]
            Product,

            [EnumDesc("产品类别同步")]
            ProductCate,

            [EnumDesc("企业收货地址")]
            EntAddr,
        }
       
        #endregion

        #region  产品
        /// <summary>
        /// 产品类型
        /// </summary>
        public enum ProductType
        {
            /// <summary>
            /// 试剂
            /// </summary>
            [EnumDesc("试剂")]
            Reagent=1,
            /// <summary>
            /// 仪器
            /// </summary>
            [EnumDesc("仪器")]
            Instrument = 2,
            
        }
       #endregion

        #region 订单
        /// <summary>
        /// 订单类型
        /// </summary>
        public enum OrderType
        { 
            /// <summary>
            /// 预订单
            /// </summary>
            [EnumDesc("预订单")]
            PreOrder = 0,
            /// <summary>
            /// 正常订单
            /// </summary>
            [EnumDesc("正常订单")]
            NormalOrder = 1,
            /// <summary>
            /// 缺货订单
            /// </summary>
            [EnumDesc("空库存订单")]
            EmptyOrder = 2,
        }
        /// <summary>
        /// 订单状态
        /// </summary>
        public enum OrderStatus
        { 
            [EnumDesc("已提交")]
            Submit = 0,
            [EnumDesc("仓库已分配库存")]
            CompanyConfirm = 1,
            [EnumDesc("客户确认订单")]
            CustomerConfirm = 2,
            [EnumDesc("订单发运安排中")]
            DeliveryProcessing = 3,
            [EnumDesc("订单已出库")]
            DeliveryComplete = 4,
            [EnumDesc("交易成功")]
            Success = 5,
            [EnumDesc("订单取消")]
            Cancel = 6, 
        }
        /// <summary>
        /// 空订单处理状态
        /// </summary>
        public enum OrderEmptyStatus
        {
            [EnumDesc("待补货")]
            Pending = 0,
            [EnumDesc("补货中")]
            Processing = 1,
            [EnumDesc("补货完成")]
            Complete = 2
        }

        public enum OrderServiceStatus
        { 
            [EnumDesc("待调用")]
            Pending = 0,
            [EnumDesc("成功")]
            Success = 1,
            [EnumDesc("失败")]
            Fail = 2,
        }
        #endregion

        #region 投诉类型
        /// <summary>
        /// 投诉类型
        /// </summary>
        public enum OrderComplaintType
        { 
            [EnumDesc("产品相关")]
            Product,
            [EnumDesc("服务相关")]
            Service,
            [EnumDesc("产品相关")]
            Logistics,
             [EnumDesc("售后相关")]
            SaleAfter,
             [EnumDesc("其他相关")]
            Other,
        }
        /// <summary>
        /// 投诉状态
        /// </summary>
        public enum OrderComplaintStatus
        { 
            [EnumDesc("已提交")]
            Pending,
            [EnumDesc("处理中")]
            Processing,
            [EnumDesc("已关闭")]
            Close,
        }
        #endregion

        #region 客户类别分类方式
        /// <summary>
        /// 客户类别分类方式
        /// </summary>
        public enum CustomerProductCateType
        {
            /// <summary>
            /// 按品牌
            /// </summary>
            ByBrand=1,
            /// <summary>
            /// 按功能
            /// </summary>
            ByFunction = 2,
        }
        #endregion

    }

    #region Attribute
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)]
    public class EnumDescAttribute : Attribute
    {
        private string enumDisplayText;
        private int enumRank;
        private FieldInfo fieldIno;

        public EnumDescAttribute(string enumDisplayText, int enumRank)
        {
            this.enumDisplayText = enumDisplayText;
            this.enumRank = enumRank;
        }


        public EnumDescAttribute(string enumDisplayText)
            : this(enumDisplayText, 5) { }

        public string EnumDisplayText
        {
            get { return this.enumDisplayText; }
            set { this.EnumDisplayText = value; }
        }

        public int EnumRank
        {
            get { return enumRank; }
        }

        public int EnumValue
        {
            get { return (int)fieldIno.GetValue(null); }
        }

        public string FieldName
        {
            get { return fieldIno.Name; }
        }

        #region  =========================================对枚举描述属性的解释相关函数

        /// <summary>
        /// 排序类型
        /// </summary>
        public enum SortType
        {
            /// <summary>
            ///按枚举顺序默认排序
            /// </summary>
            Default,
            /// <summary>
            /// 按描述值排序
            /// </summary>
            DisplayText,
            /// <summary>
            /// 按排序
            /// </summary>
            Rank
        }

        private static System.Collections.Hashtable cachedEnum = new Hashtable();


        public static string GetEnumText(Type enumType)
        {
            EnumDescAttribute[] eds = (EnumDescAttribute[])enumType.GetCustomAttributes(typeof(EnumDescAttribute), false);
            if (eds.Length != 1) return string.Empty;
            return eds[0].EnumDisplayText;
        }

        public static string GetFieldText(object enumValue)
        {
            EnumDescAttribute[] descriptions = GetFieldTexts(enumValue.GetType(), SortType.Default);
            foreach (EnumDescAttribute ed in descriptions)
            {
                if (ed.fieldIno.Name == enumValue.ToString()) return ed.EnumDisplayText;
            }
            return string.Empty;
        }

        public static EnumDescAttribute[] GetFieldTexts(Type enumType)
        {
            return GetFieldTexts(enumType, SortType.Default);
        }


        public static EnumDescAttribute[] GetFieldTexts(Type enumType, SortType sortType)
        {
            EnumDescAttribute[] descriptions = null;

            if (cachedEnum.Contains(enumType.FullName) == false)
            {
                FieldInfo[] fields = enumType.GetFields();
                ArrayList edAL = new ArrayList();
                foreach (FieldInfo fi in fields)
                {
                    object[] eds = fi.GetCustomAttributes(typeof(EnumDescAttribute), false);
                    if (eds.Length != 1) continue;
                    ((EnumDescAttribute)eds[0]).fieldIno = fi;
                    edAL.Add(eds[0]);
                }

                cachedEnum.Add(enumType.FullName, (EnumDescAttribute[])edAL.ToArray(typeof(EnumDescAttribute)));
            }
            descriptions = (EnumDescAttribute[])cachedEnum[enumType.FullName];
            if (descriptions.Length <= 0) throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");

            //按指定的属性冒泡排序
            for (int m = 0; m < descriptions.Length; m++)
            {
                //默认就不排序了
                if (sortType == SortType.Default) break;

                for (int n = m; n < descriptions.Length; n++)
                {
                    EnumDescAttribute temp;
                    bool swap = false;

                    switch (sortType)
                    {
                        case SortType.Default:
                            break;
                        case SortType.DisplayText:
                            if (string.Compare(descriptions[m].EnumDisplayText, descriptions[n].EnumDisplayText) > 0) swap = true;
                            break;
                        case SortType.Rank:
                            if (descriptions[m].EnumRank > descriptions[n].EnumRank) swap = true;
                            break;
                    }

                    if (swap)
                    {
                        temp = descriptions[m];
                        descriptions[m] = descriptions[n];
                        descriptions[n] = temp;
                    }
                }
            }

            return descriptions;
        }

        #endregion
    }

    #endregion
}
